package com.jeecg.p3.divination.web.back;

import java.util.Date;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.velocity.VelocityContext;
import org.jeecgframework.p3.core.common.utils.AjaxJson;
import org.jeecgframework.p3.core.util.SystemTools;
import org.jeecgframework.p3.core.util.WeiXinHttpUtil;
import org.jeecgframework.p3.core.util.plugin.ViewVelocity;
import org.jeecgframework.p3.core.utils.common.PageQuery;
import org.jeecgframework.p3.core.utils.common.StringUtils;
import org.jeecgframework.p3.core.web.BaseController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.jeecg.p3.divination.def.DivinationProperties;
import com.jeecg.p3.divination.entity.WxActDivination;
import com.jeecg.p3.divination.service.WxActDivinationService;

 /**
 * 描述：</b>WxActDivinationController<br>
 * @author junfeng.zhou
 * @since：2016年03月23日 16时02分02秒 星期三 
 * @version:1.0
 */
@Controller
@RequestMapping("/divination/back/wxActDivination")
public class WxActDivinationController extends BaseController{
  @Autowired
  private WxActDivinationService wxActDivinationService;
  
  private static String domain = DivinationProperties.domain;
/**
  * 列表页面
  * @return
  */
@RequestMapping(value="list",method = {RequestMethod.GET,RequestMethod.POST})
public void list(@ModelAttribute WxActDivination query,HttpServletResponse response,HttpServletRequest request,
			@RequestParam(required = false, value = "pageNo", defaultValue = "1") int pageNo,
			@RequestParam(required = false, value = "pageSize", defaultValue = "10") int pageSize) throws Exception{
		VelocityContext velocityContext = new VelocityContext();
		String viewName = "divination/back/wxActDivination-list.vm";
		try {
		 	PageQuery<WxActDivination> pageQuery = new PageQuery<WxActDivination>();
		 	pageQuery.setPageNo(pageNo);
		 	pageQuery.setPageSize(pageSize);
		 	String jwid =  request.getSession().getAttribute("jwid").toString();
		 	String defaultJwid = DivinationProperties.defaultJwid;
		 	if(defaultJwid.equals(jwid)){
		 		String createBy = request.getSession().getAttribute("system_userid").toString();;
		 		query.setCreateBy(createBy);
		 	}
		 	query.setJwid(jwid);
			pageQuery.setQuery(query);
			velocityContext.put("wxActDivination",query);
			velocityContext.put("pageInfos",SystemTools.convertPaginatedList(wxActDivinationService.queryPageList(pageQuery)));
		} catch (Exception e) {
			e.printStackTrace();
		}
		ViewVelocity.view(request,response,viewName,velocityContext);
}

 /**
  * 详情
  * @return
  */
@RequestMapping(value="toDetail",method = RequestMethod.GET)
public void wxActDivinationDetail(@RequestParam(required = true, value = "id" ) String id,HttpServletResponse response,HttpServletRequest request)throws Exception{
		VelocityContext velocityContext = new VelocityContext();
		String viewName = "divination/back/wxActDivination-detail.vm";
		WxActDivination wxActDivination = wxActDivinationService.queryById(id);
		velocityContext.put("wxActDivination",wxActDivination);
		ViewVelocity.view(request,response,viewName,velocityContext);
}

/**
 * 跳转到添加页面
 * @return
 */
@RequestMapping(value = "/toAdd",method ={RequestMethod.GET, RequestMethod.POST})
public void toAddDialog(HttpServletRequest request,HttpServletResponse response,ModelMap model)throws Exception{
	 VelocityContext velocityContext = new VelocityContext();
	 String viewName = "divination/back/wxActDivination-add.vm";
	 velocityContext.put("date", new Date().getTime());
	 ViewVelocity.view(request,response,viewName,velocityContext);
}

/**
 * 保存信息
 * @return
 */
@RequestMapping(value = "/doAdd",method ={RequestMethod.GET, RequestMethod.POST})
@ResponseBody
public AjaxJson doAdd(@ModelAttribute WxActDivination wxActDivination){
	AjaxJson j = new AjaxJson();
	try {
		//TODO
		wxActDivination.setActiveFlag("1");
		wxActDivinationService.doAdd(wxActDivination);
		j.setMsg("保存成功");
	} catch (Exception e) {
		e.printStackTrace();
		j.setSuccess(false);
		j.setMsg("保存失败");
	}
	return j;
}

/**
 * 跳转到编辑页面
 * @return
 */
@RequestMapping(value="toEdit",method = RequestMethod.GET)
public void toEdit(@RequestParam(required = true, value = "id" ) String id,HttpServletResponse response,HttpServletRequest request) throws Exception{
		 VelocityContext velocityContext = new VelocityContext();
		 WxActDivination wxActDivination = wxActDivinationService.queryById(id);
		 velocityContext.put("wxActDivination",wxActDivination);
		 String viewName = "divination/back/wxActDivination-edit.vm";
		 ViewVelocity.view(request,response,viewName,velocityContext);
}

/**
 * 编辑
 * @return
 */
@RequestMapping(value = "/doEdit",method ={RequestMethod.GET, RequestMethod.POST})
@ResponseBody
public AjaxJson doEdit(@ModelAttribute WxActDivination wxActDivination){
	AjaxJson j = new AjaxJson();
	try {
		//TODO
		wxActDivination.setActiveFlag("1");
		wxActDivinationService.doEdit(wxActDivination);
		j.setMsg("编辑成功");
	} catch (Exception e) {
		e.printStackTrace();
		j.setSuccess(false);
		j.setMsg("编辑失败");
	}
	return j;
}


/**
 * 删除
 * @return
 */
@RequestMapping(value="doDelete",method = RequestMethod.GET)
@ResponseBody
public AjaxJson doDelete(@RequestParam(required = true, value = "id" ) String id){
		AjaxJson j = new AjaxJson();
		try {
			wxActDivinationService.doDelete(id);
			j.setMsg("删除成功");
		} catch (Exception e) {
			e.printStackTrace();
			j.setSuccess(false);
			j.setMsg("删除失败");
		}
		return j;
}
/**
 * 获取shortUrl
 * @param id
 * @return
 */
@RequestMapping(value="getShortUrl",method = RequestMethod.POST)
@ResponseBody
public AjaxJson getShortUrl(@RequestParam(required = true, value = "id" ) String id){
	AjaxJson j=new AjaxJson();
	try {
		WxActDivination wxActDivination = wxActDivinationService.queryById(id);
		String shortUrl = wxActDivination.getShortUrl();
		if(StringUtils.isEmpty(shortUrl)){
			String hdurl=wxActDivination.getHdurl();
			hdurl = hdurl.replace("${domain}", domain);
			shortUrl=WeiXinHttpUtil.getShortUrl(hdurl,DivinationProperties.defaultJwid);
			if(StringUtils.isEmpty(shortUrl)){
				shortUrl=hdurl;
			}else{
				WxActDivination divination=new WxActDivination();
				divination.setId(wxActDivination.getId());
				divination.setShortUrl(shortUrl);
				wxActDivinationService.doEdit(divination);
			}
		}
		if(StringUtils.isEmpty(shortUrl)){
			j.setMsg("获取地址失败！");
			j.setSuccess(false);
		}else{
			j.setObj(shortUrl);
			j.setSuccess(true);
			j.setMsg("获取地址成功！");
		}
	} catch (Exception e) {
		e.printStackTrace();
		j.setMsg("获取地址失败！");
		j.setSuccess(false);
	}
	return j;
}

}

